package service

import (
	"duoke/utils/gconv"
	"errors"
	"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram"
	"github.com/gin-gonic/gin"
)

var Miniprogram = new(miniprogram)

type miniprogram struct{}

func (s *miniprogram) App(c *gin.Context) (*miniProgram.MiniProgram, error) {
	platform, err := AccountSetting.Info(c, "miniprogram")
	if err != nil {
		return nil, err
	}
	if platform == nil {
		accountR, _ := Request.Account(c)
		return nil, errors.New("小程序配置信息不存在" + gconv.String(accountR.Id))
	}
	if gconv.String(platform["app_id"]) == "" {
		return nil, errors.New("小程序appid不存在")
	}
	if gconv.String(platform["app_secret"]) == "" {
		return nil, errors.New("小程序secret不存在")
	}
	// config := yaml.GetConfig()
	return miniProgram.NewMiniProgram(&miniProgram.UserConfig{
		AppID:     gconv.String(platform["app_id"]),     // 小程序appid
		Secret:    gconv.String(platform["app_secret"]), // 小程序app secret
		HttpDebug: true,
		Log: miniProgram.Log{
			Level: "debug",
			File:  "./log/wechat.log",
		},
		// 可选，不传默认走程序内存
		//Cache: kernel.NewRedisClient(&kernel.RedisOptions{
		//	Addr:     config.Redis.Host + ":" + gconv.String(config.Redis.Port),
		//	Password: config.Redis.Password,
		//	DB:       config.Redis.Db,
		//}),
	})
}
